/*
 * Game.cpp
 *
 *  Created on: Nov 18, 2010
 *      Author: timmeh
 */

#include "Game.h"
using namespace bo;

#include <iostream>
using namespace std;
Game::Game(AbstractFactory* fact, LevelGenerator* gen, const int fps, const int lives, Player* player)
	:fFactory(fact), fLvlGen(gen){
	this->fTimer = new Stopwatch(fps);
	this->fFPS = fps;
	this->fPlayer = player;
}

Game::~Game() {
	delete this->fFactory;
	delete this->fLvlGen;
	delete this->fTimer;
}

void Game::AddLevel(Level* lev){
	this->fLevVector.push_back(lev);
}

void Game::GenStdGame(const char* paddleFile, const char* breakableFile, const char* ballFile){
	this->AddLevel(this->fLvlGen->CreateStandardLevel(breakableFile, paddleFile, ballFile));
}


void Game::Run(){
	enum Gamestate{NEXTLEVEL, GAMEOVER, QUIT, CONTINUE};
	this->fGameRunning = true;
	bool gameOver = false;
	int levelReturn = 0;
	while ( this->fGameRunning && !gameOver ){
		for (vector<Level*>::iterator it = this->fLevVector.begin(); it!=this->fLevVector.end(); ++it) {
			this->fGameRunning = true;
			while (this->fGameRunning){
				(*it)->AdvanceLevelState(levelReturn, this->fPlayer);
				switch(levelReturn){
				case NEXTLEVEL:
					// Set fGameRunning to false so we break out of the current level loop and we can advance
					this->fGameRunning = false;
					break;
				case GAMEOVER:
					gameOver = true;
					return;
				case QUIT:
					return;
				case CONTINUE:
					break;
				}
				this->fTimer->GetTick();
				(*it)->Visualize();
				this->fPlayer->Visualize();
			}
		}
	}
	return;
}
